package com.changgou.controller;

import com.changgou.dao.BrandMapper;
import com.changgou.entity.Result;
import com.changgou.entity.StatusCode;
import com.changgou.goods.domain.Brand;
import com.changgou.service.BrandService;
import com.github.pagehelper.PageInfo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

/**
 * @author codeDorado
 * @version 1.0
 * @date 2020/7/16 16:08
 */
@RestController
@RequestMapping(value = "/brand")
@CrossOrigin
@Slf4j
public class BrandController {

    @Autowired
    BrandService brandService;

    @GetMapping("getall")
    public Result<List<Brand>> getAll() {
        return new Result<List<Brand>>(true, StatusCode.OK, "success", brandService.findAll());
    }

    @GetMapping("/{id}")
    public Result<Brand> getById(@PathVariable Integer id) {
        return new Result<>(true, StatusCode.OK, "success", brandService.findById(id));
    }

    @PostMapping("/add")
    public Result add(@RequestBody Brand brand) {
        brandService.add(brand);
        return new Result(true, StatusCode.OK, "success");
    }

    @PutMapping("/update")
    public Result update(@RequestBody Brand brand) {
        brandService.update(brand);
        return new Result(true, StatusCode.OK, "success");
    }

    @DeleteMapping("/delete/{id}")
    public Result<Brand> delete(@PathVariable Integer id) {
        brandService.delete(id);
        return new Result<>(true, StatusCode.OK, "success");
    }

    @PostMapping("/search")
    public Result<List<Brand>> search(@RequestBody Brand brand) {
        List<Brand> brands = brandService.search(brand);
        return new Result(brands);
    }

    @GetMapping("/search/{page}/{size}")
    public Result<PageInfo<Brand>> findPage(@PathVariable Integer page,
                                            @PathVariable Integer size) {
        PageInfo<Brand> pageInfo = brandService.findPage(page, size);
        return new Result(pageInfo);
    }

    @PostMapping("/search/{page}/{size}")
    public Result<PageInfo<Brand>> findPage(@PathVariable Integer page,
                                            @PathVariable Integer size,
                                            @RequestBody Brand brand) {
        log.info("brand : {}", brand);
        PageInfo<Brand> pageInfo = brandService.findPage(page, size, brand);

        return new Result(pageInfo);
    }

}
